﻿#pragma once
#ifndef __PAGECACHE_H__
#define __PAGECACHE_H__

#include "Common.h"
#include <mutex>
class PageCache
{
public:
	// 这里这两个函数必须返回引用，因为他们的拷贝构造函数和operator=都被delete了

	static PageCache& GetInstance()
	{
		return _pagechche;
	}
	// 获取一个k页的Span
	Span* newSpan(size_t getSize);
	std::mutex& getMutex()
	{
		return _mutex;
	}
	// 获取从对象到span的映射
	Span* MapObjectToSpan(void* obj);

	// 释放空闲span回到Pagecache，并合并相邻的span
	void ReleaseSpanToPageCache(Span* span);
private:
	PageCache() = default;
	PageCache(const PageCache&) = delete;
	PageCache& operator=(const PageCache&) = delete;
private:
	SpanList _spanLists[NPAGES];
	static PageCache _pagechche;
	std::mutex _mutex;
	std::unordered_map<PAGEID, Span*> _idSpanMap;
};
#endif
